import { getHouseInfo } from '@/api/user';
import { pushAccounts } from '@/handler/menuHandler';
import mixinMethods from '@/utils/mixinMethods';

// 创建手动匹配小区的数据
function createManualAreaModel(portType) {
  return {
    platformResblockId: '', //平台小区id
    portResblockName: '', //端口小区名称

    portType, // 平台类型
    loading: false, //匹配小区加载
    searchKey: '', //搜索关键词

    housingEstateOptions: [], //匹配小区选项
  };
}
const state = {
  hmDelSummaryInfo: {},
  hmDelVerificationDetails: [],
  houDelRecommendVO: {},
  houDelVO: {},
  houImageVO: [],
  houPortInfo: [],

  // 手动匹配小区的数据
  manualAreaModelList: [],
};

const mutations = {
  UPDATE: (state, { key, value }) => {
    state[key] = value;
  },
  // 重置手动匹配小区数据
  INIT_AREA_MODEL: (state) => {
    state.manualAreaModelList = pushAccounts.map((item) => {
      return createManualAreaModel(item.portType)
    })
  },
  INFO_UPDATE: (state, content) => {
    const { houDelVO, houPortInfo } = content;
    content.houDelVO = mixinMethods.houDelVOFormat(houDelVO);
    content.houDelVO.matchingFacilitiesList = houDelVO.matchingFacilitiesList || [];
    content.houDelVO.portHouNum = content.houDelVO.portHouNum || "";
    
    const list = mixinMethods.getHouPortInfoFormat(houPortInfo, houDelVO, pushAccounts);

    content.houPortInfo = list;
    content.houDelVO.totalPrices = mixinMethods.getTotalPrices(houDelVO);

    for (let key in content) {
      if (state.hasOwnProperty(key)) {
        state[key] = content[key];
      }
    }
  },
  // 添加图片
  ADD_IMG: (state, data) => {
    state.houImageVO.push(data);
  },
  // 删除图片
  REMOVE_IMG: (state, fileId) => {
    let i = state.houImageVO.findIndex((v) => fileId == v.fileId);
    if (i > -1) {
      state.houImageVO.splice(i, 1)[0];
    }
  },
};

const actions = {
  async updateInfoById({ commit }, houDelId) {
    const { content, isSuccess, errorCode } = await getHouseInfo({ houDelId });
    if (isSuccess && content) {
      commit('INIT_AREA_MODEL');
      commit('INFO_UPDATE', content);
  
      return Promise.resolve(content);
    } else {
      return Promise.reject(content);
    }
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters: {
    getManualAreaModel(state) {
      return (portType) => {
        return state.manualAreaModelList.find((v) => v.portType === portType)
      }
    },
    houDelId: (state) => {
      return state.houDelVO.id;
    },
    // 室内图
    indoorList: (state) => {
      return state.houImageVO.filter((item) => item.tagCode !== 'huxingtu' && item.tagCode !== 'xiaoqu');
    },
    // 户型图 （房型图）
    houseTypeList: (state) => {
      return state.houImageVO.filter((item) => item.tagCode === 'huxingtu');
    },
    // 小区图
    circumList: (state) => {
      return state.houImageVO.filter((item) => item.tagCode === 'xiaoqu');
    },
  },
};
